<?php
namespace app\admin\controller;

use app\common\respond\HttpApi;
use app\admin\logic\FactoryLogic;

/**
 * 猪厂类
 */
class FactoryController
{
    /**
     * @Description 猪厂列表
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/03/05 12:10:01
     * @return \think\response\Json
     */
    public function list(HttpApi $respond)
    {
        $param = [
            'page' => (int)request()->get('page') ?: 1,
            'page_size' => (int)request()->get('page_size') ?: 10,
            'name' => request()->get('name'),
        ];
        $rule = [
            'page' => 'require|>:0',
            'page_size' => 'require|>:0',
            'name' => 'max:30'
        ];
        validate($rule)->check($param);

        $logic = new FactoryLogic();
        $data = $logic->getList($param);

        return $respond->success($data);
    }

    /**
     * 新增
     *
     * @param HttpApi $respond
     * @return \think\response\Json
     */
    public function add(HttpApi $respond)
    {
        $param = [
            'name' => request()->post('name'),
            'address' => request()->post('address'),
            'contacts' => request()->post('contacts'),
            'phone' => request()->post('phone'),
        ];
        $rule = [
            'name' => 'require|max:30',
            'address' => 'require|max:50',
            'contacts' => 'require|max:20',
            'phone' => 'require|mobile',
        ];
        validate($rule)->check($param);

        $logic = new FactoryLogic();
        $data = $logic->add($param['name'], $param['address'], $param['contacts'], $param['phone']);

        return $respond->success($data);
    }

    /**
     * @Description 详情
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/03/05 12:19:19
     * @return \think\response\Json
     */
    public function detail(HttpApi $respond)
    {
        $param = [
            'id' => request()->get('id')
        ];
        $rule = [
            'id' => 'require|integer|>:0'
        ];
        validate($rule)->check($param);

        $logic = new FactoryLogic();
        $data = $logic->detail($param['id']);

        return $respond->success($data);
    }

    /**
     * @Description 下拉选择
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/03/05 15:35:44
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function selectList(HttpApi $respond)
    {
        $param = [
            'page' => (int)request()->get('page') ?: 1,
            'page_size' => (int)request()->get('page_size') ?: 10,
            'name' => request()->get('name'),
        ];
        $rule = [
            'page' => 'require|>:0',
            'page_size' => 'require|>:0',
            'name' => 'max:30'
        ];
        validate($rule)->check($param);

        $logic = new FactoryLogic();
        $data = $logic->selectList($param);

        return $respond->success($data);
    }
}
